<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Meta extends Admin {

	public function __construct()
    {
        parent::__construct();
		$this->load->model("meta_model");
		$this->load->model("mcategory");
    }
	public function index()
	{
		$data = array();
		$data['getData'] = $this->meta_model->getListAll();
		$this->layout->view('admin/meta/index',$data);
	}
	public function edit($id=null){
		$data = array();

		$this->form_validation->set_rules('title', 'Title', 'required');
		$this->form_validation->set_rules('keyword', 'Keyword', 'required');
		$this->form_validation->set_rules('description', 'Description', 'required');
		$data = array();
		if($this->form_validation->run ()==TRUE ){

			$data['title'] 			= $this->input->post('title');
			$data['keyword'] 		= $this->input->post('keyword');
			$data['description'] 	= $this->input->post('description');
			$data['categoryId'] 	= $this->input->post('categoryId');
			$data['needId'] 		= $this->input->post('needId');
			$data['typeId'] 		= $this->input->post('typeId');
			$data['createdTime'] 	= time();
			$data['status'] 		= 1;
			if($this->input->post('id')){

				$data['id'] 		= $this->input->post('id');
				$metaId = $this->meta_model->update($data,$data['id']);
				redirect('admin/meta');
			}else {
				$metaId = $this->meta_model->insert($data);
				redirect('admin/meta');
			}
		}
		if(!empty($id)){
			$data['meta']= $this->meta_model->getSingle($id);
			$data['subLevel']= $this->mcategory->getSubCategory($data['meta']['categoryId']);

		}else {
			$data['meta'] = array('id'=>0,'subLevel'=>array());
		}
		$data['category']= $this->mcategory->getCategory('category');

		$this->layout->view('admin/meta/edit',$data);
	}

	public function save(){
		$data = array();
		$this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('provinder', 'provinder', 'required');
		$this->form_validation->set_rules('orderNo', 'orderNo', 'required');
		$data['flag'] = true;
		if ($this->form_validation->run() == TRUE){
			$obj['name'] = $this->input->post('name');
			$obj['provinderId'] = $this->input->post('provinder');
			$obj['orderNo'] = $this->input->post('orderNo');

			$objId = $this->district_model->insert($obj);

			if($objId) {
				$data['msg'] = "Them moi thanh cong";
			}else {
				$data['msg'] = "Loi khi them moi";
				$data['flag'] = false;
			}

		}else {
			$data['msg'] = "Thong tin sai";
			$data['flag'] = false;
		}
		echo json_encode($data);

		exit();
	}

	public function do_update(){
		$data = array();
		$data['flag'] 	= true;
		$obj['name'] 	= $this->input->post('name');
		$obj['provinderId'] 	= $this->input->post('provinder');
		$obj['orderNo'] = $this->input->post('orderNo');
		$id 			= $this->input->post('id');
		$objId = $this->district_model->update($obj,$id);
		if($objId){
			$data['msg'] = "Update thanh cong";
		}else {
			$data['msg'] = "Loi khi them moi";
			$data['flag'] = false;
		}
		echo json_encode($data);
		exit();
	}



	public function combobox($id){
		$data = $this->mcategory->getSubCategory($id);
		echo json_encode($data);
		exit();
	}

}